// #ifdef H5
const jweixin = require('./jweixin-module')
// #endif
import util from './util.js'
import Http from '@/utils/http.interceptor.js'

const initJssdk = (query, callback) => {
  let url = query.link
  Http.get(`/share/signature`, {
    url: url,
  }).then((res) => {
    const { data } = res
    jweixin.config({
      debug: false,
      appId: data.appId,
      timestamp: data.timestamp,
      nonceStr: data.noncestr,
      signature: data.signature,
      jsApiList: [
        'updateTimelineShareData',
        'updateAppMessageShareData',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
      ],
    })
    if (res.code == 1 && callback) {
      callback(data)
    }
  })
}

export const wxShare = (data, callback) => {
  if (util.isWechat()) {
    initJssdk(data, () => {
      jweixin.ready(() => {
        //自定义“分享给朋友”及“分享到QQ”按钮的分享内容
        jweixin.updateAppMessageShareData({
          title: data.title, // 分享标题
          desc: data.desc, // 分享描述
          link: data.link, // 该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致
          imgUrl: data.imgUrl, // 分享图标
          success: (res) => {
            callback(res)
          },
          cancel: (res) => {
            console.log('取消')
          },
          trigger: function (res) {
            console.log('用户点击发送给朋友')
          },
        })

        //自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容
        jweixin.updateTimelineShareData({
          title: data.title, // 分享标题
          link: data.link, // 该链接域名或路径必须与当前页面对应的公众号 JS 安全域名一致
          imgUrl: data.imgUrl, // 分享图标
          success: (res) => {
            callback(res)
          },
          cancel: (res) => {
            console.log('取消')
          },
          trigger: function (res) {
            console.log('用户点击发送给朋友圈')
          },
        })
      })
    })
  }
}
